package com.zc.action.${packageName!};

import com.zc.action.BaseAction;
import com.zc.common.AjaxUtil;
import com.zc.common.BeanUtil;
import com.zc.common.DMLoader;
<#if (lcmc?? && lcmc != '')>
import com.zc.common.MisConstants;
</#if>
import com.zc.common.MisException;
import com.zc.common.Operator;
import com.zc.common.PageInfo;
import com.zc.common.StringUtil;
<#if (zllb?? && zllb != '')>
import com.zc.common.upload.ZcFiles;
import com.zc.common.upload.ZcRequest;
import com.zc.common.upload.ZcUpload;
</#if>
import com.zc.domain.single.${tableName!};
<#if (zllb?? && zllb != '')>
import com.zc.domain.single.XM_BDXX;
import com.zc.domain.single.XM_XMXX;
</#if>
<#if (lcmc?? && lcmc != '')>
import com.zc.enobj.Ywcxspxx;
import com.zc.enobj.Ywspxx;
import com.zc.enobj.Ywtsxx;
</#if>
import com.zc.model.${packageName!}.${className?cap_first!}Facade;
<#if (lcmc?? && lcmc != '')>
import com.zc.model.yw.YwlcCall;
</#if>
import com.zc.model.yw.YwlcService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ${comments!}
 *
 * @author 董晓峰
 * @datetime ${dqsj!}
 */
@Controller(value = "${packageName!}.${className!}")
@RequestMapping(value = "/${packageName!}/${className!}/")
public class ${className?cap_first!}Action extends BaseAction {
    private static final Log log = LogFactory.getLog(${className?cap_first!}Action.class);

    @Autowired
    private ${className?cap_first!}Facade ${className!}Facade;
    <#if (lcmc?? && lcmc != '')>

    @Autowired
    private YwlcService ywlcService;
    </#if>

    /**
     * ${comments!}列表
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @RequestMapping(value = "showFrame.do")
    public String showFrame(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {

        this.icList${className?cap_first!}(request, response, model);
        return "/${packageName!}/${className!}/MainFrame";
    }

    /**
     * ${comments!}列表小页面
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @RequestMapping(value = "icList${className?cap_first!}.do")
    public String icList${className?cap_first!}(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
        PageInfo pageInfo = PageInfo.parse(request);
        Operator operator = (Operator) request.getSession().getAttribute("operator");

        String content = request.getParameter("content");
        String cgfs = request.getParameter("cgfs");
        String shzt = request.getParameter("shzt");

        Map paramMap = new HashMap();
        paramMap.put("content", content);
        paramMap.put("cgfs", cgfs);
        paramMap.put("shzt", shzt);
        List list${className?cap_first!} = this.${className!}Facade.list${className?cap_first!}(paramMap, pageInfo, operator);

        model.addAttribute("content", content);
        model.addAttribute("cgfs", cgfs);
        model.addAttribute("shzt", shzt);
        model.addAttribute("list${className?cap_first!}", list${className?cap_first!});
        model.addAttribute("pageInfo", pageInfo);
        return "/${packageName!}/${className!}/IcList${className?cap_first!}";
    }

    /**
     * 新增${comments!}
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @RequestMapping(value = "add${className?cap_first!}.do")
    public String add${className?cap_first!}(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
        String ${className!}id = StringUtil.getUUID();

        model.addAttribute("${className!}id", ${className!}id);
        return "/${packageName!}/${className!}/Edit${className?cap_first!}";
    }

    /**
     * 编辑${comments!}
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @RequestMapping(value = "edit${className?cap_first!}.do")
    public String edit${className?cap_first!}(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
        String ${className!}id = request.getParameter("${className!}id");
        if (StringUtil.isEmpty(${className!}id)) {
            throw new MisException("编辑${comments!}，${comments!}编号不能为空，请确认！");
        }
        ${tableName!} ${className!} = this.${className!}Facade.get${className?cap_first!}ByKey(${className!}id);
        if (${className!} == null) {
            throw new MisException("编辑${comments!}，${comments!}信息不存在，请确认！");
        }

        model.addAttribute("${className!}id", ${className!}id);
        model.addAttribute("${className!}", ${className!});
        return "/${packageName!}/${className!}/Edit${className?cap_first!}";
    }

    /**
     * 查看${comments!}
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @RequestMapping(value = "view${className?cap_first!}.do")
    public String view${className?cap_first!}(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
        <#if (lcmc?? && lcmc != '')>
        Operator operator = (Operator) request.getSession().getAttribute("operator");
        </#if>
        String ${className!}id = request.getParameter("${className!}id");
        if (StringUtil.isEmpty(${className!}id)) {
            throw new MisException("编辑${comments!}，${comments!}编号不能为空，请确认！");
        }
        <#if (lcmc?? && lcmc != '')>
        Ywcxspxx ywcxspxx = this.getYwspxx(MisConstants.LC_${lcmc!}, ${className!}id, operator);
        if (ywcxspxx.getYwspbz()) {
            return ywrw(ywcxspxx);
        } else {
            this.initSpjl(MisConstants.LC_${lcmc!}, ${className!}id, model, operator);

            this.edit${className?cap_first!}(request, response, model);
            return "/${packageName!}/${className!}/View${className?cap_first!}";
        }
        <#else >

        this.edit${className?cap_first!}(request, response, model);
        return "/${packageName!}/${className!}/View${className?cap_first!}";
        </#if>
    }

    /**
     * 保存${comments!}
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @RequestMapping(value = "save${className?cap_first!}.do")
    public void save${className?cap_first!}(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
        <#if (lcmc?? && lcmc != '')>
        <#else >
        Operator operator = (Operator) request.getSession().getAttribute("operator");
        </#if>
        request.setCharacterEncoding("UTF-8");
        Map resultMap = new HashMap();
        try {
            <#if (lcmc?? && lcmc != '')>
            this.psave${className?cap_first!}(request, response, model);
            <#else >
            String ${className!}id = request.getParameter("${className!}id");
            if (StringUtil.isEmpty(${className!}id)) {
                throw new MisException("保存${comments!}，${comments!}编号不能为空，请确认！");
            }
            ${tableName!} ${className!} = BeanUtil.parseBean(request, ${tableName!}.class);

            this.${className!}Facade.save${className?cap_first!}(${className!}id, ${className!}, operator);
            </#if>
            resultMap.put("status", 1);
            resultMap.put("msg", "保存成功");
            AjaxUtil.forwordJson(response, resultMap);
        } catch (MisException e) {
            resultMap.put("status", -1);
            resultMap.put("msg", "保存失败(" + e.getMessage() + ")");
            AjaxUtil.forwordJson(response, resultMap);
            log.error(e.getMessage(), e);
        } catch (Exception e) {
            resultMap.put("status", -1);
            resultMap.put("msg", "保存失败");
            AjaxUtil.forwordJson(response, resultMap);
            log.error(e.getMessage(), e);
        }
    }
    <#if (lcmc?? && lcmc != '')>

    /**
     * 保存${comments!}
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    public void psave${className?cap_first!}(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
        Operator operator = (Operator) request.getSession().getAttribute("operator");
        String ${className!}id = request.getParameter("${className!}id");
        if (StringUtil.isEmpty(${className!}id)) {
            throw new MisException("保存${comments!}，${comments!}编号不能为空，请确认！");
        }
        ${tableName!} ${className!} = BeanUtil.parseBean(request, ${tableName!}.class);

        this.${className!}Facade.save${className?cap_first!}(${className!}id, ${className!}, operator);
    }
    </#if>

    /**
     * 删除${comments!}
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @RequestMapping(value = "del${className?cap_first!}.do")
    public void del${className?cap_first!}(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
        request.setCharacterEncoding("UTF-8");
        Map resultMap = new HashMap();
        try {
            String ${className!}id = request.getParameter("${className!}id");
            if (StringUtil.isEmpty(${className!}id)) {
                throw new MisException("删除${comments!}，${comments!}编号不能为空，请确认！");
            }

            this.${className!}Facade.del${className?cap_first!}(${className!}id);
            resultMap.put("status", 1);
            resultMap.put("msg", "删除成功");
            AjaxUtil.forwordJson(response, resultMap);
        } catch (MisException e) {
            resultMap.put("status", -1);
            resultMap.put("msg", "删除失败(" + e.getMessage() + ")");
            AjaxUtil.forwordJson(response, resultMap);
            log.error(e.getMessage(), e);
        } catch (Exception e) {
            resultMap.put("status", -1);
            resultMap.put("msg", "删除失败");
            AjaxUtil.forwordJson(response, resultMap);
            log.error(e.getMessage(), e);
        }
    }

    <#if (lcmc?? && lcmc != '')>
    /**
     * 提交${comments!}
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @RequestMapping(value = "submit${className?cap_first!}.do")
    public void submit${className?cap_first!}(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {

        request.setCharacterEncoding("utf-8");
        Map resultMap = new HashMap();
        try {
            Operator operator = (Operator) request.getSession().getAttribute("operator");
            String ${className!}id = request.getParameter("${className!}id");
            if (StringUtil.isEmpty(${className!}id)) {
                throw new MisException("提交${comments!}，${comments!}编号不能为空，请确认！");
            }
            this.psave${className?cap_first!}(request, response, model);

            this.${className!}Facade.submit${className?cap_first!}(${className!}id, operator);

            resultMap.put("status", 1);
            resultMap.put("msg", "提交成功");
            AjaxUtil.forwordJson(response, resultMap);
        } catch (MisException e) {
            resultMap.put("status", -1);
            resultMap.put("msg", "提交失败(" + e.getMessage() + ")");
            AjaxUtil.forwordJson(response, resultMap);
            log.error(e.getMessage(), e);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            resultMap.put("status", -1);
            resultMap.put("msg", "提交失败");
            AjaxUtil.forwordJson(response, resultMap);
        }
    }

    /**
     * 提交${comments!}
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @RequestMapping(value = "preSubmit${className?cap_first!}.do")
    public String preSubmit${className?cap_first!}(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
        String r = ywlcService.createYwlc(MisConstants.LC_${lcmc!}, new YwlcCall() {
            @Override
            public Integer saveData() throws Exception {
                psave${className?cap_first!}(request, response, model);
                return 1;
            }

            @Override
            public void submit() throws Exception {
                submit${className?cap_first!}(request, response, model);
            }

            @Override
            public String slmc() {

                return request.getParameter("xmmc") + "-" + DMLoader.getMCByKey("BZLC", MisConstants.LC_${lcmc!});
            }

            @Override
            public String ywbh() {
                return request.getParameter("${className!}id");
            }
        }, request, response);
        return r;
    }

    /**
     * ${comments!}审核公共查询方法
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    public void icShFrame(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
        String ywbh = request.getParameter("ywbh");
        if (StringUtil.isEmpty(ywbh)) {
            throw new MisException("审核${comments!}，业务编号不能为空，请确认！");
        }
        ${tableName!} ${className!} = this.${className!}Facade.get${className?cap_first!}ByKey(ywbh);
        if (${className!} == null) {
            throw new MisException("审核${comments!}，${comments!}信息不存在，请确认！");
        }

        model.addAttribute("${className!}", ${className!});
    }

    /**
     * 审核${comments!}
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @RequestMapping(value = "shFrame.do")
    public String shFrame(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
        this.icShFrame(request, response, model);

        return "/${packageName!}/${className!}/ShFrame";
    }

    /**
     * 审核确认${comments!}
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @RequestMapping(value = "shQrFrame.do")
    public String shQrFrame(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
        this.icShFrame(request, response, model);

        return "/${packageName!}/${className!}/ShQrFrame";
    }

    /**
     * 审核编辑${comments!}
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @RequestMapping(value = "shEditFrame.do")
    public String shEditFrame(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
        this.icShFrame(request, response, model);

        return "/${packageName!}/${className!}/ShEditFrame";
    }

    /**
     * ${comments!}流程转发
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @RequestMapping(value = "forward.do")
    public String forward(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
        Ywspxx spxx = Ywspxx.parse(request);
        Operator operator = (Operator) request.getSession().getAttribute("operator");
        String ${className!}id = request.getParameter("${className!}id");
        if (StringUtil.isEmpty(${className!}id)) {
            throw new MisException("审核${comments!}，${comments!}编号不能为空，请确认！");
        }

        this.psave${className?cap_first!}(request, response, model);

        Ywtsxx ywtsxx = this.${className!}Facade.forward(${className!}id, spxx, operator);
        model.addAttribute("ywtsxx", ywtsxx);
        return MisConstants.FORWARDSUCCESS;
    }
    <#if (isYQxsq)>

    /**
     * 取消申请
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @RequestMapping(value = "qx${className?cap_first!}.do")
    public void qx${className?cap_first!}(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {

        request.setCharacterEncoding("UTF-8");
        Map resultMap = new HashMap();
        try {
            Operator operator = (Operator) request.getSession().getAttribute("operator");

            String ${className!}id = request.getParameter("${className!}id");

            if (StringUtil.isEmpty(${className!}id)) {
                throw new MisException("取消申请信息，编号不允许为空，请确认！");
            }
            this.${className!}Facade.qx${className?cap_first!}(${className!}id, operator);

            resultMap.put("status", 1);
            resultMap.put("msg", "取消申请成功");
            AjaxUtil.forwordJson(response, resultMap);
        } catch (MisException e) {
            resultMap.put("status", -1);
            resultMap.put("msg", "取消申请失败(" + e.getMessage() + ")");
            AjaxUtil.forwordJson(response, resultMap);
            log.error(e.getMessage(), e);
        } catch (Exception e) {
            resultMap.put("status", -1);
            resultMap.put("msg", "取消申请失败");
            AjaxUtil.forwordJson(response, resultMap);
            log.error(e.getMessage(), e);
        }
    }
    </#if>
    </#if>
    <#if (zllb?? && zllb != '' && zllb == 'dazl')>
    /**
     * 附件资料
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @RequestMapping(value = "ic${className?cap_first!}zl.do")
    public String ic${className?cap_first!}zl(HttpServletRequest request, ModelMap model) throws Exception {
        String ${className!}id = request.getParameter("${className!}id");
        if (StringUtil.isEmpty(${className!}id)) {
            throw new MisException("查询附件资料，${comments!}编号不能为空，请确认！");
        }

        Map m${className?cap_first!}zl = this.${className!}Facade.m${className?cap_first!}zl(${className!}id);

        model.addAttribute("m${className?cap_first!}zl", m${className?cap_first!}zl);
        return "/${packageName!}/${className!}/Ic${className?cap_first!}zl";
    }

    /**
     * 上传附件
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @RequestMapping(value = "upload${className?cap_first!}fj.do")
    public void upload${className?cap_first!}fj(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
        request.setCharacterEncoding("utf-8");
        Map resultMap = new HashMap();
        try {
            ZcUpload upload = new ZcUpload(request);
            upload.setHeaderEncoding("utf8");
            upload.upload();

            ZcFiles files = upload.getFiles();
            ZcRequest zcRequest = upload.getRequest();

            String xmid = zcRequest.getParameter("xmid");
            String zllb = zcRequest.getParameter("zllb");
            String czyid = zcRequest.getParameter("czyid");

            this.${className!}Facade.upload${className?cap_first!}fj(zllb, xmid, czyid, files);

            resultMap.put("status", 1);
            resultMap.put("msg", "上传成功");
            AjaxUtil.forwordJson(response, resultMap);
        } catch (MisException e) {
            resultMap.put("status", -1);
            resultMap.put("msg", "上传失败(" + e.getMessage() + ")");
            AjaxUtil.forwordJson(response, resultMap);
            log.error(e.getMessage(), e);
        } catch (Exception e) {
            resultMap.put("status", -1);
            resultMap.put("msg", "上传失败");
            AjaxUtil.forwordJson(response, resultMap);
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 删除附件
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @RequestMapping(value = "del${className?cap_first!}fj.do")
    public void del${className?cap_first!}fj(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map resultMap = new HashMap();
        try {
            String zlid = request.getParameter("zlid");
            if (StringUtil.isEmpty(zlid)) {
                throw new MisException("删除附件，附件编号不允许为空，请确认！");
            }
            this.${className!}Facade.del${className?cap_first!}fj(zlid);

            resultMap.put("status", 1);
            resultMap.put("msg", "删除成功");
            AjaxUtil.forwordJson(response, resultMap);
        } catch (MisException e) {
            resultMap.put("status", -1);
            resultMap.put("msg", "删除失败(" + e.getMessage() + ")");
            AjaxUtil.forwordJson(response, resultMap);
            log.error(e.getMessage(), e);
        } catch (Exception e) {
            resultMap.put("status", -1);
            resultMap.put("msg", "删除失败");
            AjaxUtil.forwordJson(response, resultMap);
            log.error(e.getMessage(), e);
        }
    }
    </#if>
}